<?php
/**
 * StopPhonesManager 
 * Version: $Id: StopPhonesManager.php,v 1.10 2008/04/09 13:59:57 master Exp $
 *
 */
class StopPhonesManager extends ORMManager {
	
	var $cities_table='passport.cities';
	var $phones_table='passport.phones2_stoplist';
	
	function __construct() {
		$this->cities_table = $GLOBALS['options']['passport']['database'].'.cities';
		$this->phones_table = $GLOBALS['options']['passport']['database'].'.phones2_stoplist';
		parent::__construct();
	}
	
	
	/**
	 * Find phone in BlackList
	 *
	 * @param string $str
	 * @return boolean (TRUE if phone is not in blacklist, FALSE if phone is in blacklist)
	 */
	function check_phones($str){
		$phonesParser = new Phones2($str);
		$phones_arr = $phonesParser->get_phones();
		
		$_SESSION['phones_arr']=$phones_arr;
		
		foreach ($phones_arr as $key=>&$item){
			
			if ($item['Valid']==1){
				$prefixes=array('380','80','0');
				foreach ($prefixes as $key=>$item2){
					
					$term=strpos($item['Val'],$item2);
					if ($term===0){
						$item['Val']=substr($item['Val'],strlen($item2));
						break;
					}
				}
				$t=$this->runSearch($item['Val']);
				if ($t) return false;
			}	
		}
		return true;
	}
	
	/**
	 * Check the phones blacklist table for the user phonenumber entire
	 *
	 * @param integer $user_id
	 * @return boolean (true if not in black list, false otherwise)
	 */
	function check_phones_by_User_id($user_id){
		$user = new UserManager();
		$user->setParam('id',$user_id);
		$user->find($user->user);
		
		$t = $this->check_phones($user->user->telefon);
		if (!$t) $_SESSION['in_black_list_'.$user_id]=$user_id;
		return $t;
	}
	
	/**
	 * Start phone numbers search...
	 *
	 * @param string $phone
	 * @return string status (true if exists in DB, false - otherwise)
	 */
	function runSearch($phone){
		$qs="SELECT phone_id, comment FROM $this->phones_table WHERE is_active and phone LIKE '$phone'";
		
		$_SESSION['black_list_def_query']=$qs;
		
		$temp=$this->getOneArray($qs);
		if ($temp['phone_id']){
				$this->setError($temp['comment']);
				return true;		
			} 
		return false;
	}
	
}
?>